<?php
/**
 * 拍卖模型
 */

namespace app\badmin\model;


use app\common\model\BaseModel;
use PDOStatement;
use think\Collection;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;

class HouseAuction extends BaseModel {

    /**
     * 获取拍卖纪录
     * @param $house_id
     * @return array|PDOStatement|string|Collection
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function getHistory($house_id) {
        $column = ['type,start_time,end_time,auction_status,market_price,consult_price,price_lower_offset,court_name,contact_name,contact_tel,source_id,source_type,enroll_num,access_num,remind_num,source_url,id as history_id,bond,final_price,bid_count,IF(type=4,pay_selloff,initial_price) as initial_price,tail_money_date'];
        $ids = self::where('house_id', $house_id)->order('id desc')->column('id');
        if ($ids) {
            $id_arr_str = join(',', $ids);
            $data = self::getDatas([['house_id', '=', $house_id]], $column, [], $id_arr_str);
            foreach ($data as $k => $v) {
                $data[$k]['typename'] = House::A_TYPES[$v['type']];
                // 待开始/进行中/成交 才有保证金
                if (in_array($data[$k]['auction_status'], [1, 2, 3])) {
                    $data[$k]['bond'] = deal_data_info($data[$k]['bond'], 2);
                } else {
                    unset($data[$k]['bond']);
                }
                //成交
                if ($data[$k]['auction_status'] == 3) {
                    $data[$k]['final_price'] = deal_data_info($data[$k]['final_price'], 2);
                } else {
                    unset($data[$k]['final_price']);
                    unset($data[$k]['bid_count']);
                }
                $data[$k]['auction_status'] = House::A_STATUSS[$v['auction_status']];

                $data[$k]['initial_price'] = deal_data_info($data[$k]['initial_price'], 2);
                $data[$k]['consult_price'] = deal_data_info($data[$k]['consult_price'], 2);
                $data[$k]['market_price'] = deal_data_info($data[$k]['market_price'], 2);
                $data[$k]['price_lower_offset'] = deal_data_info($data[$k]['price_lower_offset'], 2);
                $data[$k]['start_time'] = !empty($data[$k]['start_time']) ? date('Y/m/d H:i', $v['start_time']) : '';
                $data[$k]['end_time'] = !empty($data[$k]['end_time']) ? date('Y/m/d H:i', $v['end_time']) : '';
                unset($data[$k]['type']);
                $sourceList = House::$whereList['source_type'];
                foreach ($sourceList as $arr => $item)
                    $sourceList[$arr]['key'] == $data[$k]['source_type'] && $data[$k]['source_type'] = $sourceList[$arr]['value'];
            }
        } else {
            $data = [];
        }
        return $data;
    }

    /**
     * 筛选多条
     * @param $map
     * @param $column
     * @return array|PDOStatement|string|Collection
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function getDatas($map, $column, $order, $orderRaw) {
        $data = Db::name ('house_auction')
            ->where ($map)
            ->field ($column);
        if ($orderRaw) {
            $data = $data->orderRaw ("field(id,{$orderRaw})");
        }
        $data = $data->order ($order)
            ->select ();
        return $data;
    }

}
